/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom.generator.builder.ruleBuilder;

import java.util.ArrayList;
import java.util.Collection;
import ru.jSkyRoom.generator.command.Command;
import ru.jSkyRoom.generator.command.CompositeCommand;
import ru.jSkyRoom.generator.command.LeveledItemDistributionCommand;
import ru.jSkyRoom.generator.command.rule.CollectionAdapterRule;
import ru.jSkyRoom.generator.command.rule.Rule;
import ru.jSkyRoom.generator.command.rule.SimpleRule;
import ru.jSkyRoom.interfaces.major.LeveledItemList;
import ru.jSkyRoom.interfaces.major.Major;
import ru.rakev.matcher.Matcher;

/**
 *
 * @author Rakev
 */
public class LeveledItemsDistributerToListBuilder<T extends Major> {

    private LeveledItemList list;
    private Collection<Rule<T>> distributionRules = new ArrayList<>();

    public LeveledItemsDistributerToListBuilder(LeveledItemList list) {
        this.list = list;
    }

    public LeveledItemsDistributerToListBuilder addDistributionRule(Matcher<T> ruleMatcher) {
        return addDistributionRule(ruleMatcher, 1, 1);
    }

    public LeveledItemsDistributerToListBuilder addDistributionRule(Matcher<T> ruleMatcher, int level) {
        return addDistributionRule(ruleMatcher, level, 1);
    }

    public LeveledItemsDistributerToListBuilder addDistributionRule(Matcher<T> ruleMatcher, int level, int number) {
        LeveledItemDistributionCommand<T> distributionCommand = new LeveledItemDistributionCommand<>(list, level, number);
        Rule<T> distributionRule = new SimpleRule<>(ruleMatcher, distributionCommand);
        distributionRules.add(distributionRule);
        return this;
    }

    public Command<Collection<T>> build() {
        Collection<Rule<Collection<T>>> distributionItemsRules = new ArrayList<>(distributionRules.size());
        for (Rule<T> distributionRule : distributionRules) {
            distributionItemsRules.add(new CollectionAdapterRule(distributionRule));
        }
        return new CompositeCommand<>(distributionItemsRules);
    }
}
